****FIGURE 2****

cd "$main_dir/output/figures/main"

//--------------------------------------------------------
//Read in relevant data
//--------------------------------------------------------

//ivs
clear
use "$data_dir/clean/ivs_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
replace survey = "IVS"
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/ivs_clean_collapsed.dta", replace

//gallup
clear
use "$data_dir/clean/gallup_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/gallup_clean_collapsed.dta", replace

//wjp
clear
use "$data_dir/clean/wjp_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state, by(ccode survey) //no weights
save "$data_dir/clean/wjp_clean_collapsed.dta", replace

//ess
clear
use "$data_dir/clean/ess_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/ess_clean_collapsed.dta", replace

//lits
clear
use "$data_dir/clean/lits1_clean.dta"
forvalues i = 2/3 {
	append using "$data_dir/clean/lits`i'_clean.dta"
}
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/lits_clean_collapsed.dta", replace

//afrobarometer
clear
use "$data_dir/clean/afro1_clean.dta"
forvalues i = 2/8 {
	append using "$data_dir/clean/afro`i'_clean.dta"
}
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/afro_clean_collapsed.dta", replace

//latinobarometer
clear
use "$data_dir/clean/latino1_clean.dta"
forvalues i = 2/22 {
	append using "$data_dir/clean/latino`i'_clean.dta"
}
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/latino_clean_collapsed.dta", replace

//americasbarometer
clear
use "$data_dir/clean/americasbarometer_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/americasbarometer_clean_collapsed.dta", replace

//arab barometer
clear
use "$data_dir/clean/arab1_clean.dta"
forvalues i = 2/7 {
	append using "$data_dir/clean/arab`i'_clean.dta"
}
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/arab_clean_collapsed.dta", replace

//asiabarometer
clear
use "$data_dir/clean/asia1_clean.dta"
forvalues i = 2/5 {
	append using "$data_dir/clean/asia`i'_clean.dta"
}
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state [pw=weight], by(ccode survey)
save "$data_dir/clean/asia_clean_collapsed.dta", replace

//southasiabarometer
clear
use "$data_dir/clean/southasia1_clean.dta"
append using "$data_dir/clean/southasia2_clean.dta"
keep if survey_year>=2009 & survey_year<=2019
collapse trust_govt trust_others trust_state, by(ccode survey) //no weights
save "$data_dir/clean/southasia_clean_collapsed.dta", replace

/*-------------------------------------------------------------------------------
-------------------------------------------------------------------------------*/

use "$data_dir/clean/ivs_clean_collapsed.dta", clear
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_ivs
}

merge 1:1 ccode using "$data_dir/clean/gallup_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_gallup
}

merge 1:1 ccode using "$data_dir/clean/wjp_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_wjp
}

merge 1:1 ccode using "$data_dir/clean/afro_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_afro
}

merge 1:1 ccode using "$data_dir/clean/latino_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_latino
}

merge 1:1 ccode using "$data_dir/clean/americasbarometer_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_americas
}

merge 1:1 ccode using "$data_dir/clean/arab_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_arab
}

merge 1:1 ccode using "$data_dir/clean/lits_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_lits
}

merge 1:1 ccode using "$data_dir/clean/asia_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_asia
}

merge 1:1 ccode using "$data_dir/clean/southasia_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_southasia
}

merge 1:1 ccode using "$data_dir/clean/ess_clean_collapsed.dta", nogen keepusing(trust_govt trust_others trust_state)
local varlist trust_govt trust_others trust_state
foreach var of local varlist {
	rename `var' `var'_ess
}

count
gen n = _n/170

/*-------------------------------------------------------------------------------
-------------------------------------------------------------------------------*/

graph set window fontface "Latin Modern Math"

//Gallup and IVS
pwcorr trust_govt_gallup trust_govt_ivs, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_ivs, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(0(0.2)1,labs(small)) xlabel(0(0.2)1,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government (IVS)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_ivs.gph", replace

//Gallup and WJP
pwcorr trust_govt_gallup trust_govt_wjp, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_wjp, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government (WJP)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_wjp.gph", replace

//Gallup and Latinobarometer/Americasbarometer
cap drop trust_govt_lat_am
gen trust_govt_lat_am = trust_govt_latino
replace trust_govt_lat_am = trust_govt_americas if trust_govt_lat_am==.

pwcorr trust_govt_gallup trust_govt_lat_am, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

pwcorr trust_govt_gallup trust_govt_lat_am, sig star(.01)
scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_lat_am, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government" "(Latinobarometer/Americasbarometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_latino.gph", replace

//Gallup and Arab Barometer
pwcorr trust_govt_gallup trust_govt_arab, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_arab, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government (Arab Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_arab.gph", replace

//Gallup and LITS
pwcorr trust_govt_gallup trust_govt_lits, sig star(.01)
scalar t = round(r(C)[1,2], 0.0001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_lits, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government (Life in Transition Survey)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_lits.gph", replace

//Gallup and Asia/South Asia Barometer
cap drop trust_govt_as_sas
gen trust_govt_as_sas = trust_govt_asia
replace trust_govt_as_sas = trust_govt_southasia if trust_govt_as_sas==.

pwcorr trust_govt_gallup trust_govt_as_sas, sig star(.01)
scalar t = round(r(C)[1,2], 0.001)

scalar h = r(sig)[1,2]

if `=h'<0.01 {
	scalar p = "***"
} 
else if `=h'>0.01 & `=h'<0.05 {
	scalar p = "**"
} 
else if `=h'>0.05 & `=h'<0.1 {
	scalar p = "*"
} 
else {
	scalar p = ""
}

scatter trust_govt_gallup trust_govt_as_sas, mlabel(ccode) mlabcolor(black) mlabsize(vsmall) msize(vsmall) || lfit n n, lcolor(maroon) lpattern(dash) graphregion(color(white)) bgcolor(white) ylabel(,labs(small)) xlabel(,labs(small)) ytitle("Trust in Government (Gallup)", size(small)) xtitle("Trust in Government" "(Asia/South Asia Barometer)", size(small)) legend(off) text(0.925 0 "{&rho} = 0`=t'`=p'", place(e) size(small)) 
graph save "trust_govt_gallup_asia_sasia.gph", replace

//--------------------------------------------------------
//Create Figure 2
//--------------------------------------------------------

//combine graphs
gr combine trust_govt_gallup_ivs.gph trust_govt_gallup_wjp.gph trust_govt_gallup_latino.gph trust_govt_gallup_arab.gph trust_govt_gallup_lits.gph trust_govt_gallup_asia_sasia.gph, graphregion(color(white))
graph export "figure_2.png", replace
graph export "figure_2.tif", replace as(tif) width(3600)

erase "trust_govt_gallup_ivs.gph" 
erase "trust_govt_gallup_wjp.gph" 
erase "trust_govt_gallup_latino.gph" 
erase "trust_govt_gallup_arab.gph" 
erase "trust_govt_gallup_lits.gph" 
erase "trust_govt_gallup_asia_sasia.gph"
